package com.hxl.tech.gateway.auth.util;

import com.hxl.tech.gateway.auth.annotate.EnumCheck;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

public class EnumValidator implements ConstraintValidator<EnumCheck, Object> {

    private String enumString;

    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        String[] enumStrings = enumString.split("\\|");
        if(o != null && !Arrays.asList(enumStrings).contains(o.toString())){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public void initialize(EnumCheck constraintAnnotation) {
        enumString = constraintAnnotation.enumString();
    }
}
